import file from './file';
class group {
  private groupName: string;
  private child: group[] = [];
  private file: file[] = [];
  constructor(name: string) {
    this.groupName = name;
  }
  // 判断是否还有下级文件
  public isChild(): boolean {
    if (this.child.length > 0) {
      return true;
    }
    return false;
  }
  //获取文件组名称
  public getGroupName(): string {
    return this.groupName;
  }
  //获取child
  public getChild(): group[] {
    return this.child;
  }
  //添加文件组
  public addChild(value: group): boolean {
    for (let i = 0, length = this.child.length; i < length; i++) {
      if (this.child[i].getGroupName() === value.getGroupName()) {
        return false;
      }
    }
    this.child.push(value);
    return true;
  }
  //添加文件组群
  public addChilds(value: group[]) {
    value.forEach((e) => {
      this.addChild(e);
    });
  }
  //删除文件组
  public removeChild(value: string) {
    this.child.forEach((v, i) => {
      if (v.getGroupName() === value) {
        this.child.splice(i, 1);
        return;
      }
    });
  }
  //获取file
  public getFile(): file[] {
    return this.file;
  }
  //获取当文件组文件数量
  public getFileSize(): number {
    return this.file.length;
  }
  //获取当前文件组所有文件数量
  public getFileAllSize(): number {
    let size = this.getFileSize();
    if (this.child.length !== 0) {
      this.child.forEach((e) => {
        size += e.getFileAllSize();
      });
    }
    return size;
  }
  //添加文件
  public addFile(value: file):boolean {
    for (let i = 0, length = this.file.length; i < length; i++) {
      if (this.file[i].getFileName() === value.getFileName()) {
        return false;
      }
    }
    this.file.push(value);
    return true;
  }
  //添加文件群
  public addFiles(value: file[]) {
    value.forEach((e) => {
      this.addFile(e);
    });
  }
  //删除文件
  public removeFile(value: string) {
    this.file.forEach((v, i) => {
      if (v.getFileName() === value) {
        this.file.splice(i, 1);
        return;
      }
    });
  }
}

export default group;
